package top.lshaci.learning.activiti.day1;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mysql.cj.jdbc.Driver;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;

/**
 * EnvTest
 * 测试（集成） Activiti 环境
 * 效果：
 * 创建核心对象实例，并能够 “自动” 创建表
 *
 * @author lshaci
 * @since 1.0.0
 */
public class EnvTest {

    /**
     * 方式一：编码
     */
    @Test
    public void test1() {
        JSONObject mysqlUrlParam = JSONUtil.createObj()
                .set("createDatabaseIfNotExist", "true")
                .set("nullCatalogMeansCurrent", "true")
                .set("useUnicode", "true")
                .set("characterEncoding", "utf8")
                .set("useSSL", "false")
                .set("serverTimezone", "Asia/Shanghai")
                .set("zeroDateTimeBehavior", "convertToNull");
        StrBuilder sb = new StrBuilder();
        mysqlUrlParam.forEach((k, v) -> sb.append("&").append(k).append("=").append(v));
        System.err.println(sb.subString(1));
        // 创建流程引擎配置对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        // 数据库连接配置
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?" + sb.subString(1));
        configuration.setJdbcDriver(Driver.class.getName());
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("root");

        // 设置 “建表策略”
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        // 创建核心对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.err.println(processEngine);
    }

    /**
     * 方式二：xml
     * 在 classpath 下创建 activiti.cfg.xml 文件
     */
    @Test
    public void test2() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.err.println(processEngine);
    }
}
